package com.junxonline.plat.controller.auth;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.auth.LoginService;
import com.junxonline.plat.dao.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName: LoginController
 * @Description: 登录控制器
 * @author JunX
 */

@RestController
@RequestMapping(value = "/")
public class LoginController extends BaseController {

    @Autowired
    private LoginService loginService;

    @PostMapping(value = "/login")
    public ResultVO login(@RequestBody LoginVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getUsername()) || CommonUtils.isEmpty(vo.getPassword())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Map<String, Object> returnMap = loginService.login(vo.getUsername(), vo.getPassword());
        if (null == returnMap || returnMap.size() <= 0) {
            return getReturnError(ErrorCodeEnum.ACCOUNT_OR_PASSWORD_IS_ERROR);
        }

        return getReturnSuccess(returnMap);
    }

    @RequestMapping(path = "/401")
    public ResultVO error401() {
        return getReturnError(ErrorCodeEnum.UN_LOGIN_IS_ERROR);
    }

    @RequestMapping(path = "/403")
    public ResultVO error403() {
        return getReturnError(ErrorCodeEnum.PERMITION_IS_ERROR);
    }

}
